package com.laicunba.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.laicunba.event.CashCouponCreateEvent;
import com.laicunba.pojo.CashCouponDO;
import com.laicunba.pojo.CashCouponDefDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.CashCouponDefService;
import com.laicunba.service.CashCouponService;
import com.laicunba.service.UserService;
import com.laicunba.util.AssertUtil;

@Service
public class CashCouponDefServiceImpl extends BaseServiceImpl implements CashCouponDefService {
	@Autowired
	private UserService userService;

	@Autowired
	private CashCouponService cashCouponService;

	@Transactional
	@Override
	public CashCouponDO send2UserWithMessage(String defId, String userId, boolean justOne, String summary) {
		CashCouponDefDO defDO = get(CashCouponDefDO.class, defId);
		AssertUtil.assertNotNull(defDO, "DEF_NOT_EXIST");

		UserDO user = userService.get(UserDO.class, userId);
		AssertUtil.assertNotNull(user, "USER_NOT_EXIST");
		AssertUtil.assertTrue(!"disable".equalsIgnoreCase(user.getStatus()), "USER_DISABLED");

		AssertUtil.assertTrue(defDO.getTotalQuantity() >= defDO.getUsedQuantity(), "SEND_OUT");

		if (defDO.getBeginTime() != null && defDO.getEndTime() != null) {
			Date now = new Date();
			if (now.after(defDO.getEndTime())) {
				AssertUtil.assertFail("BEGIN_OR_ENDTIME_ERROR");
			}
		}
		CashCouponDO cashCouponDO = new CashCouponDO();
		if (justOne) {
			cashCouponDO.setId(defDO.getId() + "_" + user.getMobile());
		}

		cashCouponDO.setAmount(defDO.getAmount());
		cashCouponDO.setDefId(defDO.getId());
		cashCouponDO.setConditions(defDO.getConditions());
		cashCouponDO.setName(defDO.getName());
		cashCouponDO.setUsedAmount(BigDecimal.ZERO);
		cashCouponDO.setUserId(user.getId());
		cashCouponDO.setSummary(summary);
		cashCouponDO.setAddProfit(defDO.isAddProfit());
		if (defDO.getBeginTime() != null && defDO.getEndTime() != null) {
			cashCouponDO.setBeginTime(defDO.getBeginTime());
			cashCouponDO.setEndTime(defDO.getEndTime());
		}

		if (cashCouponDO.getEndTime() == null && defDO.getValidDay() > 0) {
			Date now = new Date();
			now.setHours(0);
			now.setMinutes(0);
			now.setSeconds(0);

			cashCouponDO.setBeginTime(new Date(now.getTime()));
			cashCouponDO.setEndTime(new Date(now.getTime()));
			cashCouponDO.getEndTime().setDate(cashCouponDO.getEndTime().getDate() + defDO.getValidDay());
		}

		AssertUtil.assertTrue(cashCouponService.create(cashCouponDO), "CASH_COUPON_CREATE_FAIL");
		defDO.setUsedQuantity(defDO.getUsedQuantity() + 1);
		AssertUtil.assertTrue(this.updateSkipCheckOwner(defDO), "UPDATE_DEF_FAIL");
		return cashCouponDO;
	}

}
